/*
 * BuilderSerializer.java
 *
 * Created on June 9, 2005, 11:46 AM
 *
 * (C) R. Alexander Milowski alex@milowski.com
 */

package com.smallx.xproc.library.saxon;

import java.net.*;
        
import net.sf.saxon.*;
import net.sf.saxon.om.NamePool;
import net.sf.saxon.om.NamespaceConstant;
import net.sf.saxon.event.*;
import net.sf.saxon.trans.XPathException;
import org.infoset.xml.Name;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.Serializer;

/**
 *
 * @author R. Alexander Milowski
 */
public class BuilderSerializer implements Serializer
{
   
   NamePool pool;
   Builder builder;
   boolean started;
   /** Creates a new instance of BuilderSerializer */
   BuilderSerializer(Configuration config,Builder builder) {
      this.builder = builder;
      this.pool = config.getNamePool();
      started = false;
   }

   public void attribute(String prefix, Name name, String value) throws XMLException
   {
      try {
         URI ns = name.getNamespaceName();
         int nmCode = ns==Name.NO_NAMESPACE ? 
                          pool.allocate(prefix,NamespaceConstant.NULL_CODE,name.getLocalName()) :
                          pool.allocate(prefix,ns.toString(),name.getLocalName());
         builder.attribute(nmCode, -1,value,0,0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }
   
   public void setUseCharacterReferences(boolean flag) {
      
   }

   public void characters(String value,boolean cdataSection) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
         builder.characters(value,0,0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void comment(String data) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
         builder.comment(data, 0,0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void endDocument() throws XMLException
   {
      try {
         builder.endDocument();
         builder.close();
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void endElement(String prefix, Name name) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
         builder.endElement();
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void namespaceDeclaration(String prefix, java.net.URI namespace) throws XMLException
   {
      try {
         builder.namespace(pool.allocateNamespaceCode(prefix, namespace.toString()),0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }

   }

   public void processingInstruction(Name target, String data) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
         builder.processingInstruction(target.getLocalName(), data,0,0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }

   }

   public void startDocument() throws XMLException
   {
      try {
         started = false;
         builder.open();
         builder.startDocument(0);
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void startElement(String prefix, Name name) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
         URI ns = name.getNamespaceName();
         int nmCode = ns==Name.NO_NAMESPACE ? 
                          pool.allocate(prefix,NamespaceConstant.NULL_CODE,name.getLocalName()) :
                          pool.allocate(prefix,ns.toString(),name.getLocalName());
         builder.startElement(nmCode, -1,0,0);
         started = true;
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }
   public void endStartElement(Name name) throws XMLException
   {
      try {
         if (started) {
            builder.startContent();
            started = false;
         }
      } catch (XPathException ex) {
         throw new XMLException(ex.getMessage(),ex);
      }
   }

   public void xmlDeclaration(String version, String encoding, String standalone) throws XMLException
   {
   }

   public void end() throws XMLException
   {
   }

   public void start() throws XMLException
   {
   }
   
   
}
